package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色Controller
 */

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer"),
            @ApiImplicitParam(name = "roleName",value = "角色名称",required = true,dataType = "String")
    })
    public ResponseResult<PageResponse<RoleVo>> getPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, String roleName){
        log.info("分页/条件查询：第{}页，共{}条，角色：{}",pageNum,pageSize,roleName);
        PageResponse<RoleVo> result = roleService.getByPage(pageNum,pageSize,roleName);
        return ResponseResult.success(result);
    }

    @PutMapping
    @ApiOperation(value = "添加角色")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO实体类",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState"})
    public ResponseResult<RoleVo> createRole(@RequestBody RoleDto roleDto) {
        log.info("新增角色：{}",roleDto.toString());
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "根据角色查询资源数据")
    @ApiImplicitParam(paramType = "path",name = "roleId",value = "角色ID", required = true,dataType = "Long")
    public ResponseResult<List<String>> findResourceByRole(@PathVariable Long roleId) {
        log.info("角色{}的资源数据",roleId);
        List<String> result = roleService.getResourceIdByRoleId(roleId);
        return ResponseResult.success(result);
    }

    @PatchMapping
    @ApiOperation(value = "修改角色")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO实体类",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.dataState","roleDto.id","checkedDeptNos",
                                              "roleName", "dataScope","checkedResourceNos"})
    public ResponseResult updateRole(@RequestBody RoleDto roleDto) {
        log.info("修改角色：{}",roleDto.toString());
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(paramType = "path",name = "roleId",value = "角色ID", required = true,dataType = "Long")
    public ResponseResult removeRole(@PathVariable Long roleId) {
        log.info("删除角色：{}",roleId);
        roleService.remove(roleId);
        return ResponseResult.success();
    }

    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框")
    public ResponseResult<List<RoleVo>> initRoles() {
        log.info("角色下拉框：");
        List<RoleVo> result = roleService.getInitRoles();
        return ResponseResult.success(result);
    }


}
